# coding:utf-8
from flask import Blueprint,request,render_template,redirect,url_for,flash
import os
from werkzeug.utils import secure_filename
desnow=Blueprint('desnow', __name__,template_folder='templates')

@desnow.route('/upload',methods=['POST','GET'])
def upload():
    if request.method == 'POST':
        f = request.files['file']
        basepath = os.path.dirname(__file__)  # 当前文件所在路径
        print(basepath)
        print(secure_filename(f.filename))
        try:
            upload_path = os.path.join(basepath,'static\\uploads',secure_filename(f.filename))  #注意：没有的文件夹一定要先创建，不然会提示没有该路径
            print(upload_path)
            f.save(upload_path)
            flash('上传{}成功!'.format(f.filename))
            return redirect(url_for('desnow.upload'))
        except:
            if secure_filename(f.filename):
                flash('上传{}失败,请重试!'.format(f.filename))
            else:
                flash('未选择任何文件!')

    return render_template('upload.html')

@desnow.route('/test')
def test():
    return 'hello,I\'m test'